package com.ruoyi.cms.util;

import com.baidu.ueditor.PathFormat;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.baidu.ueditor.upload.IStorageManager;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.oss.cloud.CloudStorageService;
import com.ruoyi.oss.cloud.OSSFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;

public class OSSStorageManager implements IStorageManager {
    public static final int BUFFER_SIZE = 8192;

    public OSSStorageManager() {
    }

    public State saveBinaryFile(byte[] data, String rootPath, String savePath) {

        CloudStorageService storage = OSSFactory.build();
        if (savePath.startsWith("/")){
            savePath = savePath.replaceFirst("/","");
        }
        int size = data.length;
        String url = storage.uploadSuffix(data, savePath);
        State  state = new BaseState(true);
        state.putInfo("url", url);
        state.putInfo("size", size);
        state.putInfo("title", savePath);
        return state;
    }

    public State saveFileByInputStream(InputStream input, long inputSize, String rootPath, String savePath) {
        CloudStorageService storage = OSSFactory.build();
        if (savePath.startsWith("/")){
            savePath = savePath.replaceFirst("/","");
        }
        int size = 0;
        try {
            size = input.available();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String url = storage.uploadSuffix(input, savePath);
        State  state = new BaseState(true);
        state.putInfo("url", url);
        state.putInfo("size", size);
        state.putInfo("title", savePath);
        return state;
    }

    private static File getTmpFile() {
        File tmpDir = FileUtils.getTempDirectory();
        String tmpFileName = (Math.random() * 10000.0D + "").replace(".", "");
        return new File(tmpDir, tmpFileName);
    }


    private static State saveInPutStream(BufferedInputStream stream, String path) {
        CloudStorageService storage = OSSFactory.build();
        String url = storage.upload(stream, path);




        State  state = new BaseState(true);

        return state;
    }

    private static State saveTmpFile(File tmpFile, String path) {




        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item=factory.createItem(tmpFile.getName(),"text/plain",true,path);
        MultipartFile multipartFile = new CommonsMultipartFile(item);

        String url = null;
        CloudStorageService storage = OSSFactory.build();
        try {
             url = storage.uploadSuffix(multipartFile.getBytes(), path);
        } catch (IOException e) {
            e.printStackTrace();
        }


        State state = null;
        File targetFile = new File(path);
        if (targetFile.canWrite()) {
            return new BaseState(false, 2);
        } else {
            try {
                FileUtils.moveFile(tmpFile, targetFile);
            } catch (IOException var5) {
                return new BaseState(false, 4);
            }

            state = new BaseState(true);
            state.putInfo("size", targetFile.length());
            state.putInfo("title", targetFile.getName());
            return state;
        }
    }

    private static State valid(File file) {
        File parentPath = file.getParentFile();
        if (!parentPath.exists() && !parentPath.mkdirs()) {
            return new BaseState(false, 3);
        } else {
            return !parentPath.canWrite() ? new BaseState(false, 2) : new BaseState(true);
        }
    }
}
